package managedbean.FRM.CashInAdvance;

import entity.CRM.Customer;
import entity.CRM.SaleOrder;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.CustomerBeanLocal;
import session.stateless.CRM.SaleOrderBeanLocal;
import session.stateless.FRM.CashInAdvanceBeanLocal;
import session.stateless.FRM.CreditAccountBeanLocal;



@ManagedBean(name="editCashInAdvanceManagerBean")
@ViewScoped
public class EditCashInAdvanceManagerBean implements Serializable {
    public EditCashInAdvanceManagerBean() {}
    
    @EJB
    private CreditAccountBeanLocal creditAccountBeanLocal;
    @EJB
    private CustomerBeanLocal customerBeanLocal;
    @EJB
    private CashInAdvanceBeanLocal cashInAdvanceBeanLocal;
    @EJB
    private SaleOrderBeanLocal saleOrderBeanLocal;
    
    private SaleOrder selectedSaleOrder;
    private Customer selectedCustomer;
    private Double cashInAdvance;
    private String statusMessage;
    
    public void initView(PhaseEvent event){
        try
        {
            if(selectedSaleOrder==null)
                selectedSaleOrder = (SaleOrder)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedSaleOrder");
            this.setSelectedCustomer(selectedSaleOrder.getCustomer());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();            
        }
    }
    
    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public SaleOrder getSelectedSaleOrder() {
        return selectedSaleOrder;
    }

    public void setSelectedSaleOrder(SaleOrder selectedSaleOrder) {
        this.selectedSaleOrder = selectedSaleOrder;
    }

    public Double getCashInAdvance() {
        return cashInAdvance;
    }

    public void setCashInAdvance(Double cashInAdvance) {
        this.cashInAdvance = cashInAdvance;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public void editCashInAdvance(ActionEvent event) throws IOException
    {
        System.out.println("test b4 edit cash in advance which is 50"+ this.getCashInAdvance());
        //cashInAdvanceBeanLocal.createCashInAdvance(selectedSaleOrder.getId(), cashInAdvance);
        cashInAdvanceBeanLocal.editCashInAdvance(selectedSaleOrder.getId(), cashInAdvance+selectedSaleOrder.getCashInAdvance().getCashInAdvanceAmount());
        statusMessage="Cash In Advance for the order with ID "+selectedSaleOrder.getId()+" has been updated to $"+saleOrderBeanLocal.findSaleOrder(selectedSaleOrder.getId()).getCashInAdvance().getCashInAdvanceAmount();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Result: " +statusMessage + ". Your credit balance has been updated to $" +customerBeanLocal.findCustomerById(selectedCustomer.getRegistrationNumber()).getCreditAccount().getCreditBalance(), "")); 
        System.out.println("test after make balance"+ customerBeanLocal.findCustomerById(selectedCustomer.getRegistrationNumber()).getCreditAccount().getCreditBalance());
        this.setSelectedSaleOrder(saleOrderBeanLocal.findSaleOrder(selectedSaleOrder.getId()));
    }
    
}
